#define  _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"

void SLInit(SL* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = 0;
	ps->size = 0;
}

void SLCheckCapacity(SL* p)
{
	assert(p);
	if (p->size == p->capacity)
	{
		int newcapacity = p->capacity == 0 ? 4 : 2 * p->capacity;

		SLDataType* tmp = (SL*)realloc(p->arr, newcapacity * sizeof(SLDataType));
		assert(tmp);

		p->arr = tmp;
		p->capacity = newcapacity;
	}
}

void print(SL* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}

void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->arr[ps->size++] = x;
}

void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	for (int i = ps->size; i > 0 ; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	ps->size++;
}

void SLDestroy(SL* ps)
{
	assert(ps);
	free(ps->arr);
	ps->arr = NULL;
	ps->size = 0;
	ps->capacity = 0;	
}

void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);

	ps->size--;
}

void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size > 0);

	for (int i = 0; i < ps->size; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

void SLInvert(SL* ps, SLDataType x, int lo)
{
	assert(ps);
	assert(lo >= 0 && lo <= ps->size);
	SLCheckCapacity(ps);

	for (int i = ps->size; i > lo ; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[lo] = x;
	ps->size++;
}

void SLErase(SL* ps, int lo)
{
	assert(ps);
	assert(lo >= 0 && lo < ps->size);

	for (int i = lo; i < ps->size; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

int SLFind(SL* ps, SLDataType x)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
		{
			return 1;
		}
	}
	return -1;
}







